
import { assetsDB } from '@/indexDB/index'
import { DEFAULT_PLATFORM_LIST, PLATFORM_LIST_KEY } from '@/views/platform/model'
import { ref } from 'vue'

export interface PlatformItem {
  id?: string | number
  /**
   * @deprecated code已弃用，请使用 id 来代替。
   */
  code?: string // 旧主键
  name: string
  remarks?: string
}

export const platformList = ref<PlatformItem[]>([])

export const initPlatformDB = async () => {
  const arr = await assetsDB.platformList.toArray()

  if (!arr.length) {

    const listStr = localStorage.getItem(PLATFORM_LIST_KEY)
    const list = listStr ? JSON.parse(listStr) : DEFAULT_PLATFORM_LIST

    await assetsDB.platformList.bulkAdd(list)
    console.log('--平台初始化成功');
    // 顺便初始化以便全局使用
    updatePlatFormList()
  } else {
    updatePlatFormList()
  }
}


export const updatePlatFormList = async () => {
  platformList.value = await assetsDB.platformList.toArray()
  return Promise.resolve(platformList.value)
}